  #This method creates the relationship config file. 
  #The key words indicationg relationships are many_to_many and one_to_many
  def create_relationship_transform_config_file
    config_file = File.new("relation_transform_config.csv","w")
    config_file.write("many_to_many,table_name_one,primary_key_of_first_table,table_name_two,primary_key_of_first_table \n")	
    config_file.write("one_to_many,one_table_name_one,primary_key_of_first_table,many_child_table_name")	
    config_file.close
  end	 


  # This method based on the keyword decided which relationship transform to execute.
  # Currently many to many and one to many relationships are supported. 
  def transform_relationships
    new_connection = create_new_connection
    FasterCSV.foreach("relation_transform_config.csv") do |row|
      if row.first == "many_to_many"
        m_to_m_transform(row[1],row[2],new_connection,row[3],row[4],new_connection)
      elsif row.first == "one_to_many"
        o_to_m_transform(row,row[1],row[2],new_connection)
      end	
    end	
  end	 

 
 #This method is the actual implementation of the many to many relationship transform.
 #The convention used for the third table is the two table names separated by an underscrore
  def m_to_m_transform(first_table,first_table_primary_key,first_table_connection,second_table,second_table_primary_key,second_table_connection)
    first_row_specs = handle_primary_key(first_table_primary_key,first_table,first_table_connection)
    second_row_specs = handle_primary_key(second_table_primary_key,second_table,second_table_connection)
    handle_primary_key(first_table_primary_key,first_table,first_table_connection)
    handle_primary_key(second_table_primary_key,second_table,second_table_connection)
    valid_table_name = generate_possible_third_table_name(first_table,second_table,first_table_connection)
    first_table_connection.execute("CREATE TABLE #{valid_table_name} (#{first_table}_#{first_table_primary_key} #{first_row_specs},#{second_table}_#{second_table_primary_key} #{second_row_specs})")
    first_table_connection.execute("ALTER TABLE #{valid_table_name} ADD PRIMARY KEY(#{first_table}_#{first_table_primary_key},#{second_table}_#{second_table_primary_key})")
  end	 



  #This is the actual implementation of the one to many relationships.
  def o_to_m_transform(row,root_table_name,root_table_primary_key,new_connection)
    primary_key_specs = handle_primary_key(root_table_primary_key,root_table_name,new_connection)        
    if primary_key_specs !=0
      for field in 3..row.length-1
        handle_primary_key(root_table_primary_key,row[field],new_connection)
        #~ new_connection.execute("ALTER TABLE #{row[field]} ADD #{root_table_primary_key} #{primary_key_specs}") 	
        puts "ALTER TABLE #{row[field]} ADD CONSTRAINT fk_const FOREIGN KEY (#{root_table_primary_key}) REFERENCES #{root_table_name}(#{root_table_primary_key}) ON DELETE CASCADE"
      end	  
    end	
  end


